home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1997 February / PC Plus Super CD (Issue 124) (PCP124-2-97) (February 1997).iso / ms / vb5cce / controls / samples / clrbox2 / clrbox2.exe / clsTimer.cls < prev    next >
Encoding:
Visual Basic class definition  |  1996-10-25  |  5.7 KB  |  134 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "clsTimer"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = False
  10. Option Explicit
  11. '-------------------------------------------------------------------------
  12. 'This class provides a Timer object and will support multiple instanciations.
  13. 'There are two key properties: Enabled, and Interval.
  14. 'Needed Files:
  15. '   modTimer.bas    Is needed because it contains the TimerProc function
  16. '                   whose address is passed in the SetTimer API.
  17. '   clsTmLnk.cls    Is needed because there must not be internal references
  18. '                   of clsTimer.  Instead of adding a reference of clsTimer
  19. '                   to a collection for the TimerProc function to access,
  20. '                   clsTimer creates an instance of clsTimerLink holds a
  21. '                   a reference and adds a reference of it to the global
  22. '                   collection for the use of TimerProc.  TimerProc can
  23. '                   then trigger clsTimerLink which will trigger clsTimer
  24. '-------------------------------------------------------------------------
  25.  
  26. Private Const mlDEFAULT_INTERVAL As Long = 1
  27.  
  28. Private mlTimerID As Long           'The ID of the system timer created by this object
  29. Private mlInterval As Long          'The interval of this timer
  30. Private mbTimerStarted As Boolean   'If true, a system timer is set for this object
  31. Private mbEnabled As Boolean        'Equals the enabled property of this object
  32.  
  33. Private WithEvents moTimerLink As clsTimerLink  'clsTimerLink object that can raise
  34. Attribute moTimerLink.VB_VarHelpID = -1
  35.                                                 'an event to this object
  36.  
  37. Public Event Timer()
  38.  
  39. '***********************
  40. 'Public Properties
  41. '***********************
  42.  
  43. Public Property Let Interval(lInterval As Long)
  44.     '-------------------------------------------------------------------------
  45.     'Purpose:   Changes the interval of the Timer
  46.     'In:        [lInterval]
  47.     '               The new interval to set the timer to.
  48.     'Effects:   [mlInterval]
  49.     '               Becomes equal to lInterval
  50.     '           Calls SetInterval only if there is a system timer corresponding
  51.     '           to this object
  52.     '-------------------------------------------------------------------------
  53.     If mlInterval <> lInterval Then
  54.         mlInterval = lInterval
  55.         If mbTimerStarted Then
  56.             SetInterval lInterval, mlTimerID
  57.         End If
  58.     End If
  59. End Property
  60.  
  61. Public Property Get Interval() As Long
  62.     Interval = mlInterval
  63. End Property
  64.  
  65. Public Property Let Enabled(bEnabled As Boolean)
  66.     '-------------------------------------------------------------------------
  67.     'Purpose:   Starts a system timer if bEnabled is true
  68.     '           Stops the timer if bEnabled is false
  69.     'Effects:   [mbEnabled] is set equal to bEnabled
  70.     '           [mbTimerStarted] is set to true if StartTimer succeeds
  71.     '                            is set to false if StopTimer succeeds
  72.     '           If true a new system timer is started and the TimerID
  73.     '           is stored in a class level variable so that this object
  74.     '           can effect the specific system timer.
  75.     '           This object then instanciates a clsTimerLink object and
  76.     '           adds it to a global collection using the TimerID converted
  77.     '           to a string a key.  The a reference to clsTimerLink object
  78.     '           is stored as a class level with events variable.
  79.     '           Timer proc will call the clsTimerLink object which will
  80.     '           raise an event to this object which will handle the event
  81.     '           and raise it to what ever object instanciated this object.
  82.     '           clsTimerLink is used because an internal reference of this
  83.     '           object can not be stored or the Terminate event will not
  84.     '           fire when the object that instanciated this object destroys
  85.     '           ist reference.
  86.     '           The callback function will receive TimerID's and will
  87.     '           only raiseevent to the corresponding object
  88.     '           If bEnable is false the system timer is killed and
  89.     '           this object is removed from the global collection
  90.     '-------------------------------------------------------------------------
  91.     Dim lReturn As Long
  92.     
  93.     mbEnabled = bEnabled    'Even if calling KillTimer fails
  94.                             'This object will stop raising events
  95.     
  96.     If bEnabled <> mbTimerStarted Then
  97.         If bEnabled Then
  98.             mlTimerID = StartTimer(mlInterval)
  99.             If mlTimerID <> 0 Then
  100.                 mbTimerStarted = True
  101.                 Set moTimerLink = New clsTimerLink
  102.                 gcTimerObjects.Add moTimerLink, Str$(mlTimerID)
  103.             End If
  104.         Else
  105.             lReturn = StopTimer(mlTimerID)
  106.             If lReturn = 1 Then
  107.                 mbTimerStarted = False
  108.                 gcTimerObjects.Remove Str$(mlTimerID)
  109.             End If
  110.         End If
  111.     End If
  112. End Property
  113.  
  114. Public Property Get Enabled() As Boolean
  115.     Enabled = mbTimerStarted
  116. End Property
  117.  
  118. Private Sub Class_Initialize()
  119.     'Make sure a gcTimerObjects collection is instanciated
  120.     If gcTimerObjects Is Nothing Then Set gcTimerObjects = New Collection
  121.     mlInterval = mlDEFAULT_INTERVAL
  122. End Sub
  123.  
  124. Private Sub Class_Terminate()
  125.     'Make sure the system timer is killed or a GPF will occur when
  126.     'the system calls a function on a dying process
  127.     If mbTimerStarted Then Enabled = False
  128. End Sub
  129.  
  130. Private Sub moTimerLink_Tick()
  131.     'Raise the Timer event to the object that instanciated Me.
  132.     RaiseEvent Timer
  133. End Sub
  134.